package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.entity.Message;
import com.choichumgming.rentcat.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping(value = "/")
public class ChatController {

    /**
     * 向指定窗口推送消息
     *
     * @param toId    接收方ID
     * @param message 消息
     * @return
     */
    @PostMapping("/push/{toId}")
    public CommonResult push(@PathVariable("toId") String toId, @RequestBody Message message) {
        try {
            WebsocketServerEndpoint endpoint = new WebsocketServerEndpoint();
            log.info(message.toString());
            endpoint.sendTo(toId, message);
            return CommonResult.success("");
        } catch (GlobalException e) {
            e.printStackTrace();
            return CommonResult.success(500, e.getMsg());
        }
    }

}
